#!/bin/bash

# how many days of history to process
PERIOD=3
DBUSER=openstack
DBPASS=openstack
DBNAME=vmstats

start_date=`date -d "$PERIOD days ago" +%Y-%m-%d`
end_date=`date +%Y-%m-%d`

echo "Running analysis for the last $PERIOD days, i.e. from $start_date to $end_date" 

while read -a pair
do
    vm=${pair[0]}
    host=${pair[1]}
    echo
    echo "Moving $vm to $host"
    instance=`nova list | grep $vm | cut -d \  -f 2`
    echo Running \"nova live-migration $instance $host\"
    echo <<'EOM'
    Remove echo above and this message when you system fully supports live migrations
    
    nova live-migration $instance $host
    if [ $? -eq 0 ]
        then
        echo Correcting historical data
        mysql -u$DBUSER -p$DBPASS $DBNAME -e "update vmstats set host=$host where vm=$vm;" 
   
    else
        echo Failed to migrate $vm to $host
    fi
EOM
done < <(/usr/local/bin/mover.r $start_date $end_date)


